package org.waveplayer.section.decoration;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.TypedValue;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;

/**
 * 绘制文本 Section
 */
public class TextSectionPainter implements SectionPainter {

    private final Paint paint;
    private Paint.FontMetrics fontMetrics;

    private int dividerHeight;
    private int sectionBarHeight;
    private int textOffset;

    private int sectionBarColor;
    private int dividerColor;
    private int textColor;

    private TextSectionPainter() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    private void setTextSizeInternal(int size) {
        paint.setTextSize(size);
        fontMetrics = paint.getFontMetrics();
    }

    @Override
    public int getSectionBarHeight() {
        return sectionBarHeight;
    }

    @Override
    public int getDividerHeight() {
        return dividerHeight;
    }

    @Override
    public void drawSectionBar(@NonNull Canvas canvas, int left, int top, int right, int bottom, @NonNull Section section) {
        paint.setColor(sectionBarColor);
        canvas.drawRect(left, top, right, bottom, paint);
        paint.setColor(textColor);
        canvas.drawText(section.title, left + textOffset, top + (sectionBarHeight / 2f) - (fontMetrics.ascent + fontMetrics.descent) / 2f, paint);
    }

    @Override
    public void drawDivider(@NonNull Canvas canvas, int left, int top, int right, int bottom) {
        paint.setColor(dividerColor);
        canvas.drawRect(left, top, right, bottom, paint);
    }

    public static class Builder {

        private final Context context;

        private int dividerHeight;
        private int sectionBarHeight;
        private int textOffset;

        private int sectionBarColor;
        private int dividerColor;
        private int textColor;

        private int textSize;

        public Builder(@NonNull Context context) {
            this.context = context;

            dividerHeight = 1;
            sectionBarHeight = dip2px(40);
            textOffset = dip2px(16);

            sectionBarColor = 0xfff6f6f6;
            dividerColor = 0xffdddddd;
            textColor = 0xff222222;

            textSize = dip2px(15);
        }

        public Builder setDividerHeight(int height) {
            this.dividerHeight = height;
            return this;
        }

        public Builder setSectionBarHeight(int height) {
            this.sectionBarHeight = height;
            return this;
        }

        public Builder setDividerColor(@ColorInt int color) {
            this.dividerColor = color;
            return this;
        }

        public Builder setSectionBarColor(@ColorInt int color) {
            this.sectionBarColor = color;
            return this;
        }

        public Builder setTextColor(@ColorInt int color) {
            this.textColor = color;
            return this;
        }

        public Builder setTextOffset(int offset) {
            this.textOffset = offset;
            return this;
        }

        public Builder setTextSize(int size) {
            this.textSize = size;
            return this;
        }

        public TextSectionPainter build() {
            TextSectionPainter painter = new TextSectionPainter();
            painter.dividerHeight = dividerHeight;
            painter.sectionBarHeight = sectionBarHeight;
            painter.textOffset = textOffset;

            painter.sectionBarColor = sectionBarColor;
            painter.dividerColor = dividerColor;
            painter.textColor = textColor;

            painter.setTextSizeInternal(textSize);
            return painter;
        }

        private int dip2px(float dp) {
            return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                    context.getResources().getDisplayMetrics());
        }
    }

}
